<template>
  <div class="baseInfor">
    <el-card>
      <el-button type="primary" @click="changePhone">更换手机号</el-button>
      <div>
        <h2>{{formData.phoneNum}}（{{formData.personName}}）</h2>
      </div>
      <el-row :gutter="10" class="row">
        <el-col :xs="6" :sm="6">
          <div class="bg">
            <label>会员等级：</label>
            <span>{{formData.gradeName}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div class="bg">
            <label>积分：</label>
            <span>{{formData.accountIntegral}} 个</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div class="bg">
            <label>储值：</label>
            <span>￥{{formData.accountBalance}}</span>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="row">
        <el-col :xs="6" :sm="6">
          <div>
            <label>会员ID：</label>
            <span>{{formData.personId}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div>
            <label>性别：</label>
            <span>{{formData.sex == "00" ?'男':'女'}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div>
            <label>生日：</label>
            <span>{{formData.birthday}}</span>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="10" class="row">
        <el-col :xs="6" :sm="6">
          <div>
            <label>注册渠道：</label>
            <span>{{formData.registerChannel | registerChannel}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div>
            <label>注册门店：</label>
            <span>{{formData.shopName}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div>
            <label>注册时间：</label>
            <span>{{formData.registerTime}}</span>
          </div>
        </el-col>
        <el-col :xs="6" :sm="6">
          <div>
            <label>最近登录时间：</label>
            <span>{{formData.lastLoginTime}}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog title="更换手机号" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row :gutter="10" class="row">
          <el-col :xs="24" :sm="24">
            <el-form-item label="原手机号：" prop="phoneNum">
              <el-input v-model="ruleForm.phoneNum" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24">
            <el-form-item label="验证码：" prop="code">
              <el-col :xs="24" :sm="24" style="display:flex">
                <el-input v-model="ruleForm.code" style="margin-right:5px"></el-input>
                <el-button type="primary" @click="sendCode" v-show="show">发送验证码</el-button>
                <el-button type="primary" v-show="!show">{{count}}</el-button>
              </el-col>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24">
            <el-form-item label="新手机号：" prop="newPhoneNum">
              <el-input v-model="ruleForm.newPhoneNum"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handlClose">取 消</el-button>
        <el-button type="primary" @click="handlSure" :loading="loadingSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { isMobile } from "@/util/validate.js";
import {
  updatePhone,
  sendMessage,
  personBasicInfoList,
} from "@/api/shop/member/list.js";
export default {
  filters: {
    registerChannel(val) {
      let obj = "";
      switch (val) {
        case "00":
          obj = "线下";
          break;
        case "01":
          obj = "线上";
          break;
      }
      return obj;
    },
  },
  data() {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("手机号不能为空"));
      } else {
        if (isMobile(value)) {
          callback();
        } else {
          return callback(new Error("请输入正确的手机号"));
        }
      }
    };
    var checkcode = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("验证码不能为空"));
      } else {
        if (value.length != 6) {
          return callback(new Error("请输入6位有效验证码"));
        } else {
          callback();
        }
      }
    };
    return {
      count: "",
      timer: null,
      show: true,
      dialogVisible: false,
      ruleForm: {},
      loading: false,
      loadingSure: false,
      rules: {
        phoneNum: [{ required: true, validator: checkPhone, trigger: "blur" }],
        code: [{ required: true, validator: checkcode, trigger: "blur" }],
        newPhoneNum: [
          { required: true, validator: checkPhone, trigger: "blur" },
        ],
      },
      formData: {},
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      personBasicInfoList({ personId: this.$route.query.personId }).then(
        (val) => {
          console.log(val);
          this.formData = val.data.data;
          this.$store.commit("SET_MEMBERINFO", this.formData);
          this.ruleForm.phoneNum = this.formData.phoneNum;
        }
      );
    },
    handlClose() {
      this.dialogVisible = false;
      this.loadingSure = false;
      clearInterval(this.timer);
      this.timer = null;
      this.show = true
      this.$refs["ruleForm"].resetFields();
    },
    handlSure() {
      this.loadingSure = true;
      this.ruleForm.personId = this.$route.query.personId;
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          updatePhone(this.ruleForm).then((val) => {
            this.getList();
            this.handlClose();
            this.$message({
              message: "修改成功",
              type: "success",
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      }).catch(()=>{
        this.loadingSure = false
      });
    },
    sendCode() {
      sendMessage({ phone: this.ruleForm.phoneNum }).then((val) => {
        const TIME_COUNT = 60;
        if (!this.timer) {
          this.count = TIME_COUNT;
          this.show = false;
          this.timer = setInterval(() => {
            if (this.count > 0 && this.count <= TIME_COUNT) {
              this.count--;
            } else {
              this.show = true;
              clearInterval(this.timer);
              this.timer = null;
            }
          }, 1000);
        }
      });
    },
    changePhone() {
      this.dialogVisible = true;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
.baseInfor {
  .row {
    margin: 30px 0px;
  }
  .bg {
    width: 202px;
    height: 33px;
    text-align: center;
    line-height: 33px;
    background-color: rgba(242, 242, 242, 1);
    font-weight: 400;
    font-style: normal;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.647058823529412);
  }
}
</style>